package oj.hot100;

public class 反转链表 {
    public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode reverseList(ListNode head) {
        //没有节点或者只有一个节点
        if(head == null || head.next == null) {
            return head;
        }
        ListNode pre = head,cur = head.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        //头变尾,记得指向null.
        head.next = null;
        return pre;
    }
}
